package kcms.ppt.dao.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import kcms.db.Ppt;
import kcms.db.PptAndContent;
import kcms.db.PptTeaContent;
import kcms.db.Pptcontent;
import kcms.ppt.dao.PptDao;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
@Repository
public class PptDaoImpl extends HibernateDaoSupport implements PptDao{

	@Resource
	private SessionFactory sessionFactory;
	@Resource
	private Ppt ppt;
	@Resource
	private Pptcontent pptcontent;
	@Resource
	public void setMySessionFactory(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	public int queryPptId(String cid,String csn,String chapter){
		List<Ppt> ppt=(List<Ppt>) this.getHibernateTemplate().find("from Ppt where cid=? and csn=? and chapter=?",cid,csn,chapter);
		if(ppt.isEmpty())
			return -1;
		else
			return ppt.get(0).getPptid();
	}
	
	public Pptcontent deletePptResourceByPptid(int pptid){
		try{
		List<Pptcontent> p=(List<Pptcontent>)this.getHibernateTemplate().find("from Pptcontent where pptid=?",pptid);
			if(p.isEmpty())
				return null;
			else{
				this.getHibernateTemplate().delete(p.get(0));
				return p.get(0);
			}
		}catch(Exception e){
		e.printStackTrace();
		return null;
		}
	}
	
	public Ppt createNewPptid(String cid,String csn,String chapter){
		try{
			ppt.setChapter(chapter);
			ppt.setCid(cid);
			ppt.setCsn(csn);
			this.getHibernateTemplate().save(ppt);
			return ppt;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public boolean saveContentRecord(Integer pptid, String generatedname,
			String originalname, String description){
		try{
			pptcontent.setDescription(description);
			pptcontent.setName(originalname);
			pptcontent.setPptid(pptid);
			pptcontent.setResource(generatedname);
			pptcontent.setUploadtime(new Date());
			this.getHibernateTemplate().save(pptcontent);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PptAndContent> listPpt(String substring, String substring2,
			String search, String startdate, String enddate, int currentpage,
			int pagesize){
		if (currentpage==0||pagesize==0){
			currentpage=1;
			pagesize=10;
		}
		String hql="from PptAndContent c where c.pptid like '%' ";
		if (substring!="-"&&substring2!="-"){
			hql+="and c.cid in ("+substring+") and c.csn in ("+substring2+") ";
		}
		if (search!=null&&!search.isEmpty()){
			hql+="and c.name like '%"+search+"%' ";
		}
		if(startdate!=null&&!startdate.isEmpty()&&enddate!=null&&!enddate.isEmpty()){
			hql+="and c.uploadtime < '"+enddate+"' and c.uploadtime > '"+startdate+"' ";
		}
		List<PptAndContent> p=(List<PptAndContent>) this.getHibernateTemplate().find(hql);
		//p.subList(((currentpage-1)*pagesize>=p.size())?p.size():(currentpage-1)*pagesize+1, toIndex)
		return p.subList((currentpage-1)*pagesize, (((currentpage-1)*pagesize<=p.size())&&(currentpage*pagesize>=p.size()))?p.size():currentpage*pagesize);
	}
	public Pptcontent getRealFileNameByPptid(String pptid){
		List<Pptcontent> pc=(List<Pptcontent>) this.getHibernateTemplate().find("from Pptcontent p where p.pptid='"+pptid+"'");
		return pc.get(0);
	}
	@SuppressWarnings("unchecked")
	public List<PptTeaContent> listPptByTid(String id){
		return (List<PptTeaContent>) this.getHibernateTemplate().find("from PptTeaContent p where p.tid=?", id);
	}
	
	public boolean deletePptByPptid(String pptid){
		try{
			if(this.getHibernateTemplate().find("from Ppt p where p.pptid='"+pptid+"'")!=null){
				this.getHibernateTemplate().delete(this.getHibernateTemplate().find("from Ppt p where p.pptid='"+pptid+"'").get(0));
				return true;
			}else
				return false;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteAllPptStatusByPptid(String pptid){
		try{
			if(this.getHibernateTemplate().find("from Pptstatus p where p.id.pptid='"+pptid+"'")!=null){
				this.getHibernateTemplate().deleteAll(this.getHibernateTemplate().find("from Pptstatus p where p.id.pptid='"+pptid+"'"));
				return true;
			}else
				return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	public Ppt getPpt() {
		return ppt;
	}
	public void setPpt(Ppt ppt) {
		this.ppt = ppt;
	}
	public Pptcontent getPptcontent() {
		return pptcontent;
	}
	public void setPptcontent(Pptcontent pptcontent) {
		this.pptcontent = pptcontent;
	}
	
}
